package org.example.TankGan.service;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.example.TankGan.pojo.Boom;
import org.example.TankGan.pojo.Bullet.Bullet;
import org.example.TankGan.pojo.Enemy.EnemyTank;
import org.example.TankGan.pojo.MyHeroTank.MyHero;
import org.example.Utils.DrawAll;
import org.example.Utils.EnemiesInit;
import org.example.Utils.JydgeIsBoom;
import org.example.Utils.Myheroinit;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author cc
 * @date 2021/4/17 11:31
 * @Description 坦克大战生成类
 */
@Data
@AllArgsConstructor

public class PanelTank extends JPanel implements KeyListener,Runnable{


    //定义我的坦克
    MyHero myHero = null;

    //敌人坦克
    Vector<EnemyTank> enemyTanks =null;

    //定义一个boom的vector用于存放炸弹    //子弹击中对象，加入一个爆炸对象
    Vector<Boom> booms = new Vector<>();

    public PanelTank(){
        /**玩家自己设置**/
        myHero = Myheroinit.initMe(259,259,15,0,60,5);

        /**敌人设置**/
        enemyTanks = EnemiesInit.inits(2,5,7,40);
            /**废弃方法**/
/**
//        for (int i = 0; i < enemyTankNums; i++) {
//            enemyTanks.add(new EnemyTank(((100+x_E)*(i+1)),((50+y_E)),2,1));
            //敌人设置初始化
//            EnemiesInit.init();
//            EnemyTank enemyTank=  new EnemyTank(((100+x_E)*(i+1)),((50+y_E)),2,1,1);//坦克初始xy，方向，速度
//            Bullet bullet = new Bullet(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDrection(), enemyTank.getSpeed() + 20);//设置子弹参数
//            Bullet bullet2 = new Bullet(enemyTank.getX() + 20, enemyTank.getY() + 80, enemyTank.getDrection(), enemyTank.getSpeed() + 20);//设置子弹参数
//            enemyTank.getBullets().add(bullet);
//            enemyTank.getBullets().add(bullet2);
//            new Thread(bullet).start();
//            new Thread(bullet2).start();
//            enemyTanks.add(enemyTank);
//        }
    }
 **/
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画坦克背景
        g.fillRect(0,0,1000,750);

        /**玩家**/
        //画自己坦克-封装方法
        DrawAll.DrawTankMyHero(myHero,g);
       //画玩家子弹
        DrawAll.DrawBullets(myHero,g);

        /**敌人**/
       //画敌人坦克并且判断敌人坦克是以及子弹否还存在
        DrawAll.DrawAllEnemyTank(enemyTanks,g);

        //画爆炸方法；
        DrawAll.DrawBooms(booms,g,this);

        //画背景右边坦克
        DrawAll.drawInfomation(g);
        /**废弃方法**/
/**        for (int j=0;j<booms.size();j++){
//            if (temp==0){
//                booms.add(new Boom(myHero.getX(),myHero.getY()));
//                temp++;
////            }
//        }
//        for (int i = 0; i < booms.size(); i++) {
//            Boom boom = booms.get(i);
//            if (boom.getLife()>8){
//                DrawAll.DrawBoom(boom.getX(),boom.getY(),g,initBoom.BoomStrat(),this);
//            }else if (boom.getLife()>6){
//                DrawAll.DrawBoom(boom.getX(),boom.getY(),g,initBoom.boomprocess(),this);
//            }else if (boom.getLife()>3){
//                DrawAll.DrawBoom(boom.getX(),boom.getY(),g,initBoom.boomprocess_2(),this);
//            }else {
//                DrawAll.DrawBoom(boom.getX(),boom.getY(),g,initBoom.boomprocessEnd(),this);
//            }
//            //减少炸弹生命值
//            boom.reduceLive();
//            //如果boom life 为0 删除炸弹效果
//            if (boom.getLife()==0){
//                booms.remove(boom);
////                if (!enemyTanks.get(i).isLive()){
////                    enemyTanks.remove(i);
////                }
//            }
//        }
    }

//    private void drawEnemiesTank(Graphics g) {
//        for (int i = 0; i < enemyTanks.size(); i++) {
//            //取出坦克
//            EnemyTank enemyTank = enemyTanks.get(i);
//            //判断敌人是否存活
//            if (enemyTank.isLive()) { //纯在则画敌人，不纯在就消失
//                DrawAll.DrawTankEnemy(enemyTank,g);
//                //取出敌人子弹
//                DrawAll.DrawBullets(enemyTank,g);
//                for (int j = 0; j < enemyTank.getBullets().size(); j++) {
//                    Bullet bullet = enemyTank.getBullets().get(j);
//                    if (bullet.isLive) {
//                        //画敌人子弹
////                        drawTank.DrawBullet(bullet.getX(), bullet.getY(), bullet.getDirection(), 1, g);
//                        drawbullet_test(enemyTank,g);
//                    } else {
//                        enemyTank.getBullets().remove(bullet);
//                    }
//                }
//            }
//        }
//    }

//    private void drawBullet(Graphics g) {
//        if ((myHero.getBullet()!=null&&myHero.getBullet().isLive==true)){
//            drawTank.DrawBullet(myHero.getBullet().getX(),myHero.getBullet().getY(),myHero.getDrection(),0,g);
//        }
//    }
//    private void drawbullet_test(Tank tank,Graphics g){
//        if (tank!=null) {
//            if (tank.getType() == 0) {
//                MyHero myHero = (MyHero) tank;
//                if ((myHero.getBullet() != null && myHero.getBullet().isLive == true)) {
//                    drawAll.DrawBullet(myHero.getBullet().getX(), myHero.getBullet().getY(), myHero.getDrection(), 0, g);
//                }
//            }else {
//                EnemyTank enemyTank = (EnemyTank) tank;
//                for (int i=0;i<enemyTank.getBullets().size();i++){
//                    if (enemyTank.getBullets().get(i).isLive){
//                        drawAll.DrawBullet(enemyTank.getBullets().get(i).getX(),enemyTank.getBullets().get(i).getY(),enemyTank.getDrection(), 1, g);
//                    }else {
//                        enemyTank.getBullets().remove(i);
//                    }
//                }
//            }
//        }
//
//    }

//    private void drwMytank(Graphics g) {
//        drawAll.DrawTank(myHero.getX(),myHero.getY(),g,myHero.getDrection(),0);
//    }
**/
    }

    /**
     * 监听事件
     * @author sosmcc
     * @param
     */
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getExtendedKeyCode()==KeyEvent.VK_S){
            myHero.setDrection(2);
            if (myHero.getY()+60<710){
                myHero.downmove();
            }
        }else if (e.getExtendedKeyCode()==KeyEvent.VK_W ){
            myHero.setDrection(0);
            if (myHero.getY()>0){
                myHero.Upmove();
            }
        }else if (e.getExtendedKeyCode()==KeyEvent.VK_A){
            myHero.setDrection(3);
            if (myHero.getX()>0){
                myHero.leftMpve();
            }
        }else if (e.getExtendedKeyCode()==KeyEvent.VK_D){
            myHero.setDrection(1);
            if (myHero.getX()+60<985){
                myHero.rightMove();
            }
        }
        if (e.getExtendedKeyCode()==KeyEvent.VK_J){
            if (myHero.getNumBullet()>1) {
                myHero.ShutManyBullet();
            }else {
                if (myHero.getBullet()==null||!myHero.getBullet().isLive)
                    myHero.ShutBullet();
            }
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {

        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //判断是否命中敌人
            JydgeIsBoom.JudgeAll(myHero,enemyTanks,booms);
            /**弃用方法**/
//            if (myHero.getBullet()!=null&&myHero.getBullet().isLive()){//我的子弹是否存在
//                for (int i = 0; i < enemyTanks.size(); i++) {
//                   if (enemyTanks.get(i).isLive()){
////                       JydgeIsBoom.JudgeIsBoom(myHero.getBullet(), enemyTanks.get(i), this.booms);
//                       JydgeIsBoom.JudgeIsBoom_Test(myHero,enemyTanks.get(i),booms);
//                   }else {
//                       enemyTanks.remove(i);
//                   }
//                }
//            }
            //判断敌人是否命中自己
            JydgeIsBoom.MyHeroisBomm(myHero,enemyTanks,booms);
            this.repaint();
        }
    }
}
